home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mgr / lib / term.c < prev    next >
C/C++ Source or Header  |  1990-09-27  |  13KB  |  615 lines

  1. /*                        Copyright (c) 1987 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: term.c,v 4.3 88/07/01 09:33:49 bianchi Exp $
  9.     $Source: /tmp/mgrsrc/lib/RCS/term.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /tmp/mgrsrc/lib/RCS/term.c,v $$Revision: 4.3 $";
  12.  
  13. /* routines for writing to mgr terminal emulator */
  14.  
  15. #include "term.h"
  16. #include "restart.h"
  17.  
  18. FILE    *m_termout;
  19. FILE    *m_termin;
  20. int    m_flags;
  21. int    m_envcount = 0;
  22. int    m_saveenvcount = 0;
  23. char    m_escchar = ESC;
  24. char    m_menuchar = M_DELIM;
  25.  
  26. jmp_buf _env;
  27.  
  28. struct sgttyb    sgtty__save[TTYMAX];
  29. int        sgtty_cnt = 0;
  30. char        m_linebuf[MAXLINE];
  31. static char    *m_fields[16];
  32.  
  33. /******************************************************************************
  34.  *
  35.  *    setup
  36.  */
  37.  
  38. int
  39. m_setup(flags)
  40. int flags;
  41.    {
  42.    m_flags = flags;
  43.  
  44. #ifdef atarist
  45.    _binmode(1);
  46. #endif
  47.  
  48.    if (!(m_flags&M_DEBUG)) {
  49.       m_termout = fopen(M_DEVICEOUT,"w");
  50.       m_termin = fopen(M_DEVICEIN,"r");
  51.       }
  52.  
  53.    if (m_termin == NULL || m_termout == NULL) 
  54.       m_flags |= M_DEBUG;
  55.  
  56.    if (m_flags&M_DEBUG) {
  57.       m_termin = stdin;
  58.       m_termout = stdout;
  59.       }
  60.    return(m_flags);
  61.    }
  62.  
  63. /******************************************************************************
  64.  *
  65.  *    get generic window parameters
  66.  */
  67.  
  68. int
  69. get_info(type,list)
  70. int type;
  71. char **list;
  72.    { 
  73.    if (type > G_MAX )
  74.       return(-1);
  75.    switch( type ) {
  76.    case G_ALL:
  77.    case G_ALLMINE:
  78.     return(-1);
  79.    }
  80.    _m_ttyset();
  81.    m_getinfo(type);
  82.    m_gets(m_linebuf);
  83.    _m_ttyreset();
  84.    return  parse(m_linebuf,list); 
  85.    }
  86.  
  87. /******************************************************************************
  88.  *
  89.  *    read window parameters off of standard input
  90.  */
  91.  
  92. int
  93. get_windata(windatap)
  94. struct window_data *windatap;
  95.    { 
  96.    if( parse(m_gets(m_linebuf),m_fields) < 8 )
  97.     return 0;
  98.    windatap->x = atoi(m_fields[0]);
  99.    windatap->y = atoi(m_fields[1]);
  100.    windatap->w = atoi(m_fields[2]);
  101.    windatap->h = atoi(m_fields[3]);
  102.    strcpy(windatap->tty,m_fields[4]);
  103.    windatap->num = atoi(m_fields[5]);
  104.    windatap->status = *m_fields[6];
  105.    windatap->setid = atoi(m_fields[7]);
  106.    return 1;
  107. }
  108.  
  109. /******************************************************************************
  110.  *
  111.  *    Get window parameters, one window at a time.
  112.  *    Returns 1 if window_data structure has been filled, 0 otherwise.
  113.  *    It is important to call get_eachwin() in a tight loop that doesn't
  114.  *    ever exit, so that all the data is picked up.
  115.  */
  116.  
  117. int
  118. get_eachwin( windatap )
  119. struct window_data *windatap;
  120.    { 
  121.    static int i = 0;
  122.  
  123.    if( !i ) {
  124.       _m_ttyset();
  125.       m_getinfo(G_ALL);
  126.    }
  127.    i = get_windata( windatap );
  128.    if( !i )
  129.       _m_ttyreset();
  130.    return(i);
  131.    }
  132.  
  133.  
  134. /******************************************************************************
  135.  *
  136.  *    Get window parameters for the current window set, one window at a time.
  137.  *    Returns 1 if window_data structure has been filled, 0 otherwise.
  138.  *    It is important to call get_eachcleintwin() in a tight loop that
  139.  *    doesn' tever exit, so that all the data is picked up.
  140.  */
  141.  
  142. int
  143. get_eachclientwin( windatap )
  144. struct window_data *windatap;
  145.    { 
  146.    static int i = 0;
  147.  
  148.    if( !i ) {
  149.       _m_ttyset();
  150.       m_getinfo(G_ALLMINE);
  151.    }
  152.    i = get_windata( windatap );
  153.    if( !i )
  154.       _m_ttyreset();
  155.    return(i);
  156.    }
  157.  
  158. /******************************************************************************
  159.  *
  160.  *    Get all window parameters.
  161.  *    NOTE CAREFULLY: The array of window_data structures pointed to by
  162.  *    list must be more than the total number of windows on the screen;
  163.  *    not a robust technique.
  164.  *    get_eachwin() is recommended above this.
  165.  */
  166.  
  167. int
  168. get_all(list)
  169. struct window_data *list;
  170.    { 
  171.    register int i;
  172.  
  173.    for(i=0;  get_eachwin( list );  i++ )
  174.       list++;
  175.    return(i);
  176.    }
  177.  
  178. /******************************************************************************
  179.  *
  180.  *    Get window parameters for client windows.
  181.  *    NOTE CAREFULLY: The array of window_data structures pointed to by
  182.  *    list must be more than the total number of windows on the screen;
  183.  *    not a robust technique.
  184.  *    get_eachclientwin() is recommended above this.
  185.  */
  186.  
  187. int
  188. get_client(list)
  189. struct window_data *list;
  190.    { 
  191.    register int i;
  192.  
  193.    _m_ttyset();
  194.    m_getinfo(G_ALLMINE);
  195.    for(i=0;  get_windata( list );  i++ )
  196.       list++;
  197.    _m_ttyreset();
  198.    return(i);
  199.    }
  200.  
  201. /******************************************************************************
  202.  *
  203.  *    get the window size
  204.  */
  205.  
  206. int
  207. get_size(x,y,wide,high)
  208. int *x, *y, *wide, *high;
  209.  
  210.    { 
  211.    register int count;
  212.  
  213.    if ((count = get_info(G_COORDS,m_fields)) >= 4) {
  214.       if (x)
  215.          *x = atoi(m_fields[0]); 
  216.       if (y)
  217.          *y = atoi(m_fields[1]); 
  218.       if (wide)
  219.          *wide = atoi(m_fields[2]); 
  220.       if (high)
  221.          *high = atoi(m_fields[3]); 
  222.       return(count);
  223.       }
  224.    else return(-count);
  225.    }
  226.  
  227. /******************************************************************************
  228.  *
  229.  *    get the mouse coords
  230.  */
  231.  
  232. int
  233. get_mouse(x,y)
  234. int *x, *y;
  235.  
  236.    { 
  237.    register int count;
  238.  
  239.    if ((count = get_info(G_MOUSE2,m_fields)) >= 3) {
  240.       if (x)
  241.          *x = atoi(m_fields[0]); 
  242.       if (y)
  243.          *y = atoi(m_fields[1]); 
  244.       return(atoi(m_fields[2]));
  245.       }
  246.    else return(-count);
  247.    }
  248.  
  249. /******************************************************************************
  250.  *
  251.  *    get system parameters
  252.  */
  253.  
  254. int
  255. get_param(host,xmax,ymax,border)
  256. char *host;
  257. int *xmax, *ymax, *border;
  258.  
  259.    { 
  260.    register int count;
  261.  
  262.    if ((count = get_info(G_SYSTEM,m_fields)) >= 4) {
  263.       if (host)
  264.          strcpy(host,m_fields[0]);
  265.       if (xmax)
  266.          *xmax = atoi(m_fields[1]); 
  267.       if (ymax)
  268.          *ymax = atoi(m_fields[2]); 
  269.       if (border)
  270.          *border = atoi(m_fields[3]); 
  271.       return(count);
  272.       }
  273.    else return(-count);
  274.    }
  275.  
  276. /******************************************************************************
  277.  *
  278.  *    get the cursor position
  279.  */
  280.  
  281. int
  282. get_cursor(x,y)
  283. int *x, *y;
  284.  
  285.    { 
  286.    register int count;
  287.  
  288.    if ((count = get_info(G_CURSOR,m_fields)) > 2) {
  289.       if (x)
  290.          *x = atoi(m_fields[0]); 
  291.       if (y)
  292.          *y = atoi(m_fields[1]); 
  293.       return(2);
  294.       }
  295.    else return(-count);
  296.    }
  297.  
  298. /******************************************************************************
  299.  *
  300.  *    get the window size - rows and columns
  301.  */
  302.  
  303. int
  304. get_colrow(cols,rows)
  305. int *cols, *rows;
  306.  
  307.    { 
  308.    register int count;
  309.  
  310.    if ((count = get_info(G_WINSIZE,m_fields)) == 2) {
  311.       if (cols)
  312.          *cols = atoi(m_fields[0]); 
  313.       if (rows)
  314.          *rows = atoi(m_fields[1]); 
  315.       return(2);
  316.       }
  317.    else return(-count);
  318.    }
  319.  
  320. /******************************************************************************
  321.  *
  322.  *    get the termcap entry
  323.  */
  324.  
  325. char *
  326. get_termcap()
  327.    { 
  328.    _m_ttyset();
  329.    m_getinfo(G_TERMCAP);
  330.    m_gets(m_linebuf);
  331.    _m_ttyreset();
  332.    return(m_linebuf);
  333.    }
  334.  
  335. /******************************************************************************
  336.  *
  337.  *    get the font size
  338.  */
  339.  
  340. int
  341. get_font(wide,high)
  342. int  *wide, *high;
  343.  
  344.    { 
  345.    register int count, result;
  346.  
  347.    if ((count = get_info(G_FONT,m_fields)) >= 3) {
  348.       if (wide)
  349.          *wide = atoi(m_fields[0]); 
  350.       if (high)
  351.          *high = atoi(m_fields[1]); 
  352.       result = atoi(m_fields[2]); 
  353.       return(result);
  354.       }
  355.    else return(-count);
  356.    }
  357.  
  358. /******************************************************************************
  359.  *
  360.  *    make a new window
  361.  */
  362.  
  363. int
  364. m_makewindow(x,y,wide,high)
  365. int  x,y,wide,high;
  366.    { 
  367.    register int count, result;
  368.    _m_ttyset();
  369.    m_newwin(x,y,wide,high);
  370.    m_gets(m_linebuf);
  371.    _m_ttyreset();
  372.    return(atoi(m_linebuf));
  373.    }
  374.  
  375. /******************************************************************************
  376.  *
  377.  *    see if window is active
  378.  */
  379.  
  380. int
  381. is_active()
  382.    { 
  383.    *m_linebuf = '\0';
  384.    get_info(G_STATUS,m_fields);
  385.    return(*m_linebuf == 'a');
  386.    }
  387.  
  388. /******************************************************************************
  389.  *
  390.  *    return last line read
  391.  */
  392.  
  393. char *
  394. m_lastline()
  395.    {
  396.    return(m_linebuf);
  397.    }   
  398.  
  399. /******************************************************************************
  400.  *
  401.  *    down load a menu
  402.  */
  403.  
  404. menu_load(n,count,text)
  405. int n;                /* menu number */
  406. int count;            /* number of menu items */
  407. struct menu_entry *text;    /* menu choices */
  408.    {
  409.    register int i, len;
  410.  
  411.    if (text == (struct menu_entry *) 0)
  412.       return (-1);
  413.  
  414.    /* calculate string lengths */
  415.  
  416.    len = 2 * count + 1;
  417.  
  418.    for (i=0;i<count;i++)
  419.        len += strlen(text[i].value) + strlen(text[i].action);
  420.    
  421.    fprintf(m_termout,"%c%d,%d%c%c",m_escchar,n,len,E_MENU,m_menuchar);
  422.  
  423.    for (i=0;i<count;i++)
  424.       fprintf(m_termout,"%s%c",text[i].value,m_menuchar);
  425.  
  426.    for (i=0;i<count;i++)
  427.       fprintf(m_termout,"%s%c",text[i].action,m_menuchar);
  428.  
  429.    m_flush();
  430.    }
  431.  
  432. /******************************************************************************
  433.  *
  434.  *    download a bitmap 
  435.  */
  436.  
  437. m_bitload(x,y,w,h,data)
  438. int x,y;
  439. int w,h;
  440. register char *data;
  441.    {
  442.    register int size = h * ((w+15)&~15)/8;        /* round to 16 bit boundary */
  443.    m_bitld(w,h,x,y,size);
  444.    while(size-- > 0)
  445.       fputc(*data++,m_termout);
  446.    m_flush();
  447.    }
  448.  
  449. /******************************************************************************
  450.  *
  451.  *    Set and save the terminal mode  (if required);
  452.  */
  453.  
  454. m_ttyset()
  455.    {
  456.     int code;
  457.    struct sgttyb buff;
  458.  
  459.    code = gtty(fileno(m_termout),sgtty__save + sgtty_cnt);
  460.  
  461.    if (sgtty__save[sgtty_cnt].sg_flags&(ECHO|RAW)) {
  462.       buff = sgtty__save[sgtty_cnt];
  463.       buff.sg_flags &= ~(ECHO|RAW);
  464.       m_flush();
  465.       stty(fileno(m_termout),&buff);
  466.       }
  467.  
  468.    if (sgtty_cnt < TTYMAX)
  469.       sgtty_cnt++;
  470.  
  471.     return(code);
  472.    }
  473.  
  474.  
  475. /******************************************************************************
  476.  *
  477.  *    Restore the terminal mode 
  478.  */
  479.  
  480. m_ttyreset()
  481.    {
  482.    if (sgtty_cnt)
  483.       sgtty_cnt--;
  484.    else
  485.       return(1);
  486.  
  487.    if (sgtty__save[sgtty_cnt].sg_flags&(ECHO|RAW)) {
  488.       m_flush();
  489.       return(stty(fileno(m_termout),sgtty__save + sgtty_cnt));
  490.       }
  491.    else
  492.       return(0);
  493.    }
  494.  
  495. /******************************************************************************
  496.  *
  497.  *    change the terminal modes
  498.  */
  499.  
  500. m_resetflags(flags)
  501.    {
  502.    struct sgttyb buff;
  503.       gtty(fileno(m_termin),&buff);
  504.       if (buff.sg_flags & flags) {
  505.          buff.sg_flags &= ~flags;
  506.      m_flush();
  507.          stty(fileno(m_termin),&buff);
  508.          }
  509.    }
  510.  
  511. m_setflags(flags)
  512.    {
  513.    struct sgttyb buff;
  514.  
  515.       gtty(fileno(m_termin),&buff);
  516.       if (!( buff.sg_flags & flags)) {
  517.          buff.sg_flags |= flags;
  518.      m_flush();
  519.          stty(fileno(m_termin),&buff);
  520.          }
  521.    }
  522.  
  523. /**
  524.     Given a bitmap id and an icon name,
  525.     have mgr load that icon into that bitmap, returning the icon width
  526.     and height via the given integer pointers.
  527.     Return a positive number if successful.
  528.     If the icon is not loaded, set the width and height values to 0 and
  529.     return 0.
  530. */
  531. int
  532. m_bitfile( bitmapid, iconname, iconwidthp, iconheightp )
  533. int    bitmapid;
  534. char    *iconname;
  535. int    *iconwidthp,
  536.     *iconheightp;
  537.    {
  538.     *iconwidthp = *iconheightp = 0;
  539.     m_bitfromfile( bitmapid, iconname );
  540.     m_flush();
  541.     return( sscanf( m_get(), "%d %d", iconwidthp, iconheightp ) == 2 );
  542.    }
  543.  
  544. /*****************************************************************************
  545.  *    parse a line into fields
  546.  */
  547.  
  548. #ifndef iswhite
  549. #define iswhite(x)    ((x)==' ' || (x)=='\t')
  550. #endif
  551.  
  552. int
  553. parse(line,fields)
  554. register char *line;
  555. register char **fields;
  556.    {
  557.    int inword = 0;
  558.    int count = 0;
  559.    char *start;
  560.    register char c;
  561.  
  562.    for(start = line;(c = *line) && c != '\n';line++)
  563.       if (inword && iswhite(c)) {
  564.          inword = 0;
  565.          *line = '\0';
  566.          *fields++ = start;
  567.          count++;
  568.          }
  569.       else if (!inword && !iswhite(c)) {
  570.          start = line;
  571.          inword = 1;
  572.          }
  573.  
  574.    if (inword) {
  575.       *fields++ = start;
  576.       count++;
  577.       if (c == '\n')
  578.          *line = '\0';
  579.       }
  580.    *fields = (char *) 0;
  581.    return(count);
  582.    }
  583.  
  584. /******************************************
  585.  *    stuff for restarting
  586.  */
  587.  
  588. _Catch()
  589.    {
  590.    ioctl(fileno(m_termin),TIOCFLUSH,0);
  591. #ifdef atarist
  592.    _discard(m_termout);
  593. #endif
  594.    longjmp(_env,1);
  595.    }
  596.  
  597. _Clean()
  598.    {
  599. #ifdef atarist
  600.    _discard(m_termout);
  601. #endif
  602.    while(m_saveenvcount < m_envcount)
  603.       m_pop();
  604.    exit(1);
  605.    }
  606.  
  607. #ifdef atarist
  608. _discard(f)
  609.    FILE *f;
  610.    {
  611.    f->_cnt = 0;
  612.    f->_ptr = f->_base;
  613.    }
  614. #endif
  615.